<?php
/**
 * 模块应用类
 * @copyright reginx.com
 * $Id: mod.class.php 1473 2012-10-09 13:01:01Z lan7 $
 */
class mod {

    /**
     * session object
     *
     * @var unknown_type
     */
    public $sobj = null;

    /**
     * module name
     *
     * @var unknown_type
     */
    public $mod  = null;

    /**
     * action name
     *
     * @var unknown_type
     */
    public $act  = null;

    /**
     * 配置信息
     *
     * @var unknown_type
     */
    public $conf = null;

    /**
     * 构造函数
     *
     */
    function __construct($param = array()){
        $this->mod = $GLOBALS['_MOD'];
        $this->act = $GLOBALS['_ACT'];
        $this->conf = &$param;
    }

    /**
     * 开启会话
     *
     */
    public function opensess($conf = array()){
        if($this->sobj === null){
            $this->sobj = sess_extra::getobj($conf ? $conf : $this->conf);
        }
    }

    /**
     * 设置模板风格
     *
     */
    private function settpldir($dir = ''){
        $dir = is_dir($dir) ? $dir : ($GLOBALS['_APP']['app_tpl_path'].$dir);
        if(is_dir($dir)){
            $GLOBALS['_TPL']->settpldir($dir);
        }else{
            core::error(core::L('no-tpl-dir'));
        }
    }

    /**
     * 获取参数
     *
     * @param unknown_type $k
     * @return unknown
     */
    public function get($k , $type = 'R'){
        return core::getgpc($k , $type);
    }
    
    /**
     * 是否存在参数
     *
     * @param unknown_type $k
     * @param unknown_type $type
     * @return boolean
     */
    public function has($k , $type = 'R'){
        return core::hasgpc($k , $type);
    }

    /**
     * 404 页面
     *
     */
    public function show404($mod = false){
        $this->setcurm($mod ? $mod : 'common');
        $this->display($this->conf['mod_404']);
    }

    /**
     * 输出消息
     *
     * @param Mixed   $msg
     * @param String  $url
     * @param Boolean $auto
     * @param Integer $time
     */
    public function showmsg($msg , $url = null , $auto = false , $time = 3){
        $url = empty($url) ? (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $this->conf['app_url']) : $url;
        $this->setcurm('common');
        $this->assign("url"  , $url);
        $this->assign('msg'  , $msg);
        $this->assign('time' , $auto ? $time : 0);
        $this->display($this->conf['mod_msg']);
    }

    /**
     * 设置当前模板缓存文件所属mod
     * @abstract 防止重复生成404/msg模板缓存文件
     *
     * @param unknown_type $mod
     */
    private function setcurm($mod=false){
        $GLOBALS['_TPL']->setcurm($mod);
    }

    /**
     * 模板变量赋值
     *
     * @param unknown_type $k
     * @param unknown_type $v
     */
    public function assign($k,$v){
        $GLOBALS['_TPL']->assign($k,$v);
    }

    /**
     * 输出模板
     *
     * @param unknown_type $tplfile
     * @param unknown_type $style
     */
    public function display($tplfile,$doctype='text/html'){
        $GLOBALS['_TPL']->display($tplfile,$doctype);
    }

    /**
     * 禁止客户端缓存当前页
     *
     */
    public function nocache(){
        header("Last-Modified: " . gmdate("D, d M Y H:i:s" ,0) . "GMT");
        header("Cache-Control: no-cache, must-revalidate");
        header("Pragma: no-cache");
    }

    /**
     * 重定向
     *
     * @param unknown_type $url
     * @param unknown_type $parse
     */
    public function redirect($url , $parse = true){
        header("HTTP/1.1 303 See Other");
        header("Location: ".($parse ? core::url($url) : $url));
        exit();
    }

    /**
     * 转发
     *
     * @param unknown_type $class
     * @param unknown_type $method
     */
    public function forward($class , $method , $extra = array()){
        $class = $class . '_mod';
        call_user_func( array(new $class($extra) , $method . 'Act'));
    }

    /**
     * Ajax输出
     * @abstract 默认不缓存
     * @param unknown_type $data
     * @param unknown_type $type
     */
    function ajaxout($data , $type='json'){
        $this->nocache();
        die($type == 'json' ? json_encode($data) : $data);
    }

    /**
     * 获取模板html
     *
     * @param unknown_type $tplFile
     * @return unknown
     */
    public function &fetchtpl($tplfile){
        return $this->tpl->display($tplfile,false);
    }

    /**
     * Token 验证
     *
     */
    function vtoken($ajax = false){
        if(!core::vtoken()){
            if(!$ajax){
                $this->showMsg('请求已失效!',$_SERVER['HTTP_REFERER'],1,3);
            }else{
                return false;
            }
        }
        return true;
    }

    /**
     * 清除表单令牌
     *
     * @return unknown
     */
    public function rmtoken(){
        if($GLOBALS['_APP']['token']){
            $key = $GLOBALS['_CFG']['app']['token_var'];
            $this->opensess($this->conf['sess']);
            $this->sobj->del($key);
        }
    }

    /**
     * 验证提交的验证码是否正确
     *
     * @return unknown
     */
    public function vcode(){
        $this->opensess($this->conf);
        return $this->sobj->get('verify') == md5(strtolower($this->get('verify' , 'P')));
    }
}
?>